<?php
class Base_Controller extends CI_Controller
{
//    public $api_base_url = "http://localhost:8888/nldou_api/";
//    public $wap_base_url = "http://localhost:8888/nldou_wap/";
//    public $web_base_url = "http://localhost:8888/nldou_web/";
    public $api_base_url = "http://api.nldou.com/";
    public $wap_base_url = "http://wap.nldou.com/";
    public $web_base_url = "http://www.nldou.com/";
    public $_uid;
    public $_utype;

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');

    }

    protected function checkapi($token,$uid)
    {
        $this->load->model('redis_model');
        $right = $this->redis_model->get('commtoken:'.$uid);
        if ($right!==$token) $this->error();
        return;
    }

    protected function success($data=array(),$msg = "sucess"){
        $result['code'] = 1;
        $result['msg'] = (string)$msg;
        $result['data'] = empty($data)?(object)array():$data;
        $this->echoJsonData($result);
        exit;
    }


    protected function error($msg = 'error',$data='') {
        $result ['code'] = 0;
        $result ['msg'] = (string)$msg;
        $result ['data'] = empty($data)?(object)array():$data;
        $this->echoJsonData($result);
        exit;
    }

    /**
     * 输出json数据
     * @param  array $data
     * @return void
     */
    protected function echoJsonData($data){
        header('Access-Control-Allow-Origin:http://wap.nldou.com');
    	header("Access-Control-Allow-Credentials:true");
        header( "Access-Control-Allow-Methods:POST,GET,PATCH,PUT,DELETE,OPTIONS" );
        header("Content-Type:text/html;charset=UTF-8");
        header("Access-Control-Allow-Headers:Origin,Accept,X-Requested-With,Content-Type,Access-Control-Request-Method,Access-Control-Request-Headers");
//        header("Access-Control-Expose-Headers:Access-Control-Allow-Origin,Origin");
        echo json_encode($data);
    }

    /**
     * 过滤输出json数据
     * @param  array|string|int $data
     * @return string|int
     */
    protected function filterJsonData($data){
        if($data === null){
            return '';
        }
        elseif (empty($data)) {
            return $data;
        } else {
            return is_array($data) ? array_map('', $data) : ($data === null ? '' : $data);
        }
    }
}
